/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306.table;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import my12306.bean.Seats;

/**
 *
 * @author Jack
 */
//public class OrderTable {
//    /*
//     * 序号 席别 	票种 	姓名 	证件类型 	证件号码 	手机号
//     */
//}
public class OrderTable extends JPanel {

    private boolean DEBUG = true;
    public JTable table;

    public OrderTable() {
        super(new GridLayout(1, 0));

        table = new JTable(new OrderTable.MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);


        TableColumn xibie = table.getColumnModel().getColumn(1);
        JComboBox xibieComboBox = new JComboBox();
        for (KeyValueItem item : Seats.allSeats().values()) {
            xibieComboBox.addItem(item);
        }
        xibieComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                KeyValueItem item = (KeyValueItem) comboBox.getSelectedItem();
                System.out.println(item.getKey() + " : " + item.getValue());
            }
        });
        xibieComboBox.setRenderer(new BasicComboBoxRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                if (value instanceof KeyValueItem) {
                    KeyValueItem entry = (KeyValueItem) value;
                    return super.getListCellRendererComponent(list, entry.getKey(), index, isSelected, cellHasFocus);
                }
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
        });
        xibie.setCellEditor(new DefaultCellEditor(xibieComboBox));


        TableColumn piaozhong = table.getColumnModel().getColumn(2);
        JComboBox piaozhongComboBox = new JComboBox();
        for (KeyValueItem item : KeyValueItem.tickets.values()) {
            piaozhongComboBox.addItem(item);
        }
        piaozhongComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                KeyValueItem item = (KeyValueItem) comboBox.getSelectedItem();
                System.out.println(item.getKey() + " : " + item.getValue());
            }
        });
        piaozhongComboBox.setRenderer(new BasicComboBoxRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                if (value instanceof KeyValueItem) {
                    KeyValueItem entry = (KeyValueItem) value;
                    return super.getListCellRendererComponent(list, entry.getKey(), index, isSelected, cellHasFocus);
                }
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
        });
        piaozhong.setCellEditor(new DefaultCellEditor(piaozhongComboBox));

        TableColumn idColumn = table.getColumnModel().getColumn(4);

        JComboBox idTypeComboBox = new JComboBox();
        for (KeyValueItem item : KeyValueItem.ids.values()) {
            idTypeComboBox.addItem(item);
        }
        idTypeComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                KeyValueItem item = (KeyValueItem) comboBox.getSelectedItem();
                System.out.println(item.getKey() + " : " + item.getValue());
            }
        });
        idTypeComboBox.setRenderer(new BasicComboBoxRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                if (value instanceof KeyValueItem) {
                    KeyValueItem entry = (KeyValueItem) value;
                    return super.getListCellRendererComponent(list, entry.getKey(), index, isSelected, cellHasFocus);
                }
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
        });
        idColumn.setCellEditor(new DefaultCellEditor(idTypeComboBox));


        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
//        add(scrollPane);
        add(table);
    }

    class MyTableModel extends AbstractTableModel {

        private String[] columnNames = {"序号", "席别", "票种", "姓名", "证件类型", "证件号码", "手机号"};
        private Object[][] data = {
            {"1", new KeyValueItem("一等", "1"), new KeyValueItem("成人票", "1"), "姓名1", new KeyValueItem("二代身份证", "1"), "", ""}, //            {"2", new KeyValueItem("二等", "2"), new KeyValueItem("儿童票", "2"), "姓名2", new KeyValueItem("一代身份证", "2"), "", ""},
        //            {"3", new KeyValueItem("三等", "3"), new KeyValueItem("学生票", "3"), "姓名3", new KeyValueItem("港澳通行证", "C"), "", ""},
        //            {"4", new KeyValueItem("四等", "4"), new KeyValueItem("成人票", "1"), "姓名4", new KeyValueItem("台湾通行证", "G"), "", ""},
        //            {"5", new KeyValueItem("五等", "5"), new KeyValueItem("残军票", "4"), "姓名5", new KeyValueItem("护照", "B"), "", ""}
        };

        public MyTableModel() {
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        @Override
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col > 0) {
                return true;
            } else {
                return false;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        @Override
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                        + " to " + value
                        + " (an instance of "
                        + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i = 0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j = 0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        OrderTable newContentPane = new OrderTable();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
        newContentPane.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
